/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.polimi.leo.jg;

import it.polimi.leo.mw.IMiddleWare;
import it.polimi.leo.mw.IMiddleWareGroup;
import it.polimi.leo.mw.IMiddleWareNode;
import it.polimi.leo.mw.IMiddlewareMessage;
import it.polimi.leo.mw.JoinGroupException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Lev
 */
public class JGFactory implements IMiddleWare {

    public IMiddleWareNode createNode(IMiddleWareGroup group) throws JoinGroupException {
        try {
            return new JGNode().joinGroup(group);
        } catch (Exception ex) {
            Logger.getLogger(JGFactory.class.getName()).log(Level.SEVERE, null, ex);
            throw new RuntimeException(ex);
        }
    }

    public IMiddlewareMessageFactory getIMiddlewareMessageFactory() {
        return new IMiddlewareMessageFactory() {
            public IMiddlewareMessage createBecomeAsuperviserMsg(IMiddleWareGroup g) {
                return new JGMessage(g,
                        IMiddlewareMessageKind.BECOME_A_SUPERVISOR,
                        "");
            }
            public IMiddlewareMessage createDataMsg(Object data) {
                return new JGMessage(null,
                        IMiddlewareMessageKind.DATA,
                        data);
            }
            public IMiddlewareMessage createGiveFitnessMsg(IMiddleWareGroup g) {
                return new JGMessage(g,
                        IMiddlewareMessageKind.GIVE_FITNESS,
                        "");
            }
        };
    }

}
